/*
	报表table组件
*/
define(
	[
		'require',
		'broada/base/text!./template.hbs'
	],
	function(require) {
		"use strict";
		var forEach = Ember.EnumerableUtils.forEach;
		var tpl = require('broada/base/text!./template.hbs');

		var ExportFormView = Ember.Component.extend({
			layout: Ember.HTMLBars.compile(tpl),
			didInsertElement: function() {
				this.todo();
			},
			contentChange: Ember.observer("content", function() {
				this.todo();
			}),

			booleChange: Ember.observer("boole", function() {

				var $viewRoot = this.$();
				if(this.get("boole")){
					$viewRoot.find(".select-menu-wrapper .select-content").text("Top10");	
				}
				var ctrl = this.get("controller");
				ctrl.set("boole",false);
			}),

			currentValueChange: Ember.observer("currentValue", function() {
				var $viewRoot = this.$();
				if(!this.get("unwanted")){
					$viewRoot.find(".select-menu-wrapper .select-content").text(this.get("currentValue"));	
				}
				
			}),
 
			todo: function() {
				var ctrl = this.get("controller"),
					view = this,
					optionLabelPath = this.get("optionLabelPath"),
					optionValuePath = this.get("optionValuePath"),
					prompt = this.get("prompt");
				ctrl.set("boole",false);
				var $viewRoot = this.$(),
					content = this.get("content"),
					selectData = [];
				if (!content) {
					return;
				}
				if (prompt) {
					var json = {};
					json[optionValuePath] = "";
					json[optionLabelPath] = prompt;
					content.unshift(json);
				}

				forEach(content, function(node) {
					selectData.push(node[optionLabelPath]);
				});
				if (view.get("currentValue")) {
					selectData.forEach(function(node, index) {
						if (view.get("currentValue") == node) {
							$viewRoot.find(".select-menu-wrapper .select-content").text(node);
						}
					});
				} else {
					$viewRoot.find(".select-menu-wrapper .select-content").text(selectData[0]);
				}

				this.set("selectData", Ember.A(selectData));
				$(".select-menu-wrapper").each(function(node) {
					$(this).on("click", function(event) {
						$(".select-menu").hide();
						$(this).find(".select-menu").show();
						event.stopPropagation();
					});
				})
				$(document).on("click", function() {
					$(".select-menu").hide();
				})
				setTimeout(function() {
					$viewRoot.find(".select-menu-wrapper").each(function() {
						var now = $(this);
						$(this).find(".select-menu span").on("click", function(event) {
							console.log(3);
							var current = $(this).html();
							now.find(".select-content").text(current).attr("title", current);
							$(".select-menu").hide();
							console.log(content);
							forEach(content, function(node) {
								if (node[optionLabelPath] == current) {
									console.log(optionValuePath);
									console.log(node[optionValuePath]);

									view.set("selectValue", node[optionValuePath]);
									console.log(view.get("selectValue"));
								}
							})
							event.stopPropagation();
						});
					});
				}, 0)
			}
		});

		return ExportFormView;
	}
);